/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.fspike.contentprovider.toolkit;

import com.compomics.fspike.contentprovider.localprovider.ResourceContentProvider;
import com.compomics.fspike.contentprovider.webprovider.GoogleContentProvider;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Kenneth
 */
public class WebImageSourceProvider {

    //TODO make this not hardcoded
    private static final String[] filter = new String[]{
        "http://bits.wikimedia.org/static-1.23wmf19/skins/common/images/magnify-clip.png",
        "http://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Commons-logo.svg/15px-Commons-logo.svg.png",
        "http://species.wikimedia.org/wiki/Special:CentralAutoLogin/start?type=1x1",
        "http://bits.wikimedia.org/images/wikimedia-button.png",
        "http://bits.wikimedia.org/static-1.23wmf20/skins/common/images/poweredby_mediawiki_88x31.png",
        "http://upload.wikimedia.org/wikipedia/en/thumb/4/4a/Commons-logo.svg/30px-Commons-logo.svg.png",
        "http://upload.wikimedia.org/wikipedia/commons/thumb/d/df/Wikispecies-logo.svg/34px-Wikispecies-logo.svg.png",
        "http://en.wikipedia.org/wiki/Special:CentralAutoLogin/start?type=1x1",
        "http://bits.wikimedia.org/static-1.23wmf20/skins/common/images/poweredby_mediawiki_88x31.png",
        "http://bits.wikimedia.org/static-1.23wmf20/skins/common/images/magnify-clip.png"
    };

    /**
     *
     * @param taxonomy the taxonomy to search an image for
     * @return a list of strings that match the input query
     */
    public static LinkedList<String> getImageURLsForTaxonomy(String taxonomy) {
        Set<String> images = new HashSet<>();
        /*      try {
         images.addAll(WikipediaContentProvider.getRelevantPicturesOnWikiMediaURL(taxonomy));
         } catch (IOException | ParserConfigurationException e) {

         }
         */
        try {
            //TODO DOUBLE CHECK THIS, FILTER SET TO IMAGES THAT CAN BE FREELY RE-USED
            images.addAll(GoogleContentProvider.getFreelyDistributableImageURLsFromGoogle(taxonomy, 10));
        } catch (MalformedURLException e) {

        } catch (IOException ex) {
            Logger.getLogger(WebImageSourceProvider.class.getName()).log(Level.SEVERE, null, ex);
        }
        /* try {
         images.addAll(WikipediaContentProvider.getRelevantPicturesOnWikiPediaURL(taxonomy));
         } catch (IOException | ParserConfigurationException e) {

         }*/

        LinkedList<String> toReturn = new LinkedList<>();
        for (String anImageURL : images) {
            //check this for correct relevant stuff
            //TODO rewrite this?
            if (!anImageURL.startsWith("http://bits.wikimedia.org/static-")) {
                toReturn.add(anImageURL);
            }
        }
        //filter out some labels and stuff 
        //TODO check if this can't be done during parsing
        for (String aBadURL : filter) {
            toReturn.remove(aBadURL);
        }
        
        if(toReturn.isEmpty()){
            try {
                toReturn.add(ResourceContentProvider.getDefaultImageURL().toString());
            } catch (IOException ex) {
                Logger.getLogger(WebImageSourceProvider.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        return toReturn;
    }

}
